package com.example.extract;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.example.common.CommonConst;
import com.example.myutil.ClientManager;

public class ExtractClassByTeacher {
	private HttpClient httpClient = new DefaultHttpClient();

	public void storeValidateImg() throws Exception {
//		HttpGet httpget1 = new HttpGet("http://121.248.70.214/jwweb/sys/ValidateCode.aspx");
//		httpClient.execute(httpget1);
//		httpget1.releaseConnection();
		HttpGet httpget = new HttpGet("http://121.248.70.214/jwweb/sys/ValidateCode.aspx");
		httpget.setHeader(new BasicHeader("Referer", "http://121.248.70.214/jwweb/ZNPK/TeacherKBFB.aspx"));
		httpget.setHeader(new BasicHeader("Accept", "image/webp,image/*,*/*;q=0.8"));
		httpget.setHeader(new BasicHeader("Accept-Encoding", "gzip, deflate, sdch"));
		httpget.setHeader(new BasicHeader("Accept-Language", "zh-CN,zh;q=0.8"));
		httpget.setHeader(new BasicHeader("Connection", "keep-alive"));
		httpget.setHeader(new BasicHeader("DNT", "1"));
		httpget.setHeader(new BasicHeader("Host", "121.248.70.214"));
		httpget.setHeader(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"));
		HttpResponse response = httpClient.execute(httpget);
		Header setCookie = response.getFirstHeader("Set-Cookie");
		String cookievalue = setCookie.getValue();
		CommonConst.cookie = cookievalue.substring(0, cookievalue.indexOf(";"));
		System.out.println(CommonConst.cookie);
		HttpEntity entity = response.getEntity();
		InputStream is = entity.getContent();
		File file = new File(CommonConst.validateImg);
		FileOutputStream fos = new FileOutputStream(file);
		byte[] buffer = new byte[1024];
		int m = 0;
		while ((m = is.read(buffer)) > -1) {
			fos.write(buffer, 0, m);
		}
		fos.flush();
		fos.close();
		//httpget.releaseConnection();
		System.out.println("stored img");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String validateCode = br.readLine().trim();
		HttpPost httpPost = new HttpPost("http://121.248.70.214/jwweb/ZNPK/TeacherKBFB_rpt.aspx");
		httpPost.setHeader(new BasicHeader("Referer", "http://121.248.70.214/jwweb/ZNPK/TeacherKBFB.aspx"));
		httpPost.setHeader(new BasicHeader("Accept", "image/webp,image/*,*/*;q=0.8"));
		httpPost.setHeader(new BasicHeader("Accept-Encoding", "gzip, deflate, sdch"));
		httpPost.setHeader(new BasicHeader("Accept-Language", "zh-CN,zh;q=0.8"));
		httpPost.setHeader(new BasicHeader("Connection", "keep-alive"));
		httpPost.setHeader(new BasicHeader("DNT", "1"));
		httpPost.setHeader(new BasicHeader("Host", "121.248.70.214"));
		httpPost.setHeader(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"));
		// httpPost.setHeader("Referer",
		// "http://121.248.70.214/jwweb/ZNPK/TeacherKBFB.aspx");
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("Sel_XNXQ", "20150"));
		params.add(new BasicNameValuePair("Sel_JS", "000163"));
		params.add(new BasicNameValuePair("type", "1"));
		params.add(new BasicNameValuePair("txt_yzm", validateCode));
		UrlEncodedFormEntity urlentity = new UrlEncodedFormEntity(params, "gb2312");
		httpPost.setEntity(urlentity);
		HttpResponse httpResponse = ClientManager.getResponse(httpPost);
		Header setCookie1 = httpResponse.getFirstHeader("Set-Cookie");
		String cookiename = setCookie1.getName();
		String cookievalue1 = setCookie.getValue();
		CommonConst.cookie = cookiename + "=" + cookievalue1.substring(0, cookievalue.indexOf(";"));
		System.out.println(CommonConst.cookie);
		HttpEntity entity1 = httpResponse.getEntity();
		System.out.println(EntityUtils.toString(entity1));
	}

	public static void main(String[] args) throws Exception {
		ExtractClassByTeacher jj = new ExtractClassByTeacher();
		jj.storeValidateImg();
	}
}
